#include "NetWorkService.h"
#include "NetCore.h"
#include "Svc_BaseTypedef.h"
#include "FilterHelper.h"
#include "NetWorkService.h"
#include "hi_appcomm_log.h"
#include "NetCapture.h"
#include "NetAudio.h"
#include "OnlineToolCallBack.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>

typedef enum {
    NET_TRANS_RATE= 1,
    NET_TRANS_QUALITY  = 2
} NetTransferState_t;



NET_TRANS_POLICY_CFG g_stNetTransPolicy;



int g_nCapture = 0;
int g_nLogicNum=0;   
bool g_bIsNetRunning=true;
bool g_bExitNetThread=FALSE;

pthread_t g_NetWork_PthreadID;

/*********************
 *函数说明:  注册捕获接口和添加全局链接人数
 *********************/


void NetCoreIoFini(struct conn *c)//注销成员
{
		unRegisterConn(c);
}

/*************************
 *函数说明:  注册捕获接口和添加全局链接人数
 **************************/
int  NetCoreIoInit(struct conn *c)  //添加USER信息，添加捕捉池成员，视频池，音频池成员
{

	if(0!=RegisterConn(c))
	{
	return -1;
	}
	return 0;

	
}





/*********************
*函数说明: Svc网络模块传输策略初始化
***********************/

void NetInitQualityRateCfg()
{

	memset(&g_stNetTransPolicy, 0 ,sizeof(NET_TRANS_POLICY_CFG));
		g_stNetTransPolicy.bNetTransPolicy = 0;
		g_stNetTransPolicy.emNetTransState = NET_TRANS_RATE;
		g_stNetTransPolicy.bHaveDisk = 0;
		g_stNetTransPolicy.bExtStream = 0;
		g_stNetTransPolicy.bHighDownload = 0;
	
}


/*
 * 函数说明:
 * 
 */

void NetCoreIoProcess(struct conn *c)
{
	assert(c->sock != -1); 
    unsigned long len;
    NetCoreRecv(c);//接收  到了remote.buff中
    len = IO_DATALEN(&c->remote);
    while(len >= sizeof(DVRIP)) 
    {	
        struct dvrip szHead;
         memset(&szHead, 0 , sizeof(struct dvrip));
         memcpy(&szHead,(char *)(c->remote.buf + c->remote.tail),sizeof(struct dvrip));
        if (len >= (szHead.dvrip_extlen + sizeof(DVRIP))) 
        {
			MsgProcess(c,((char *)c->remote.buf + c->remote.tail),szHead.dvrip_extlen + sizeof(DVRIP));
            c->remote.tail += szHead.dvrip_extlen + sizeof(DVRIP);
            len = IO_DATALEN(&c->remote);
        } 
        else 
        {
            break;
        }
    }

    if ( len > 0 )
    {
        memcpy(c->remote.buf, c->remote.buf + c->remote.tail, len);
        c->remote.head = len; 
        c->remote.tail = 0;
    }

}




/**************************
 * 函数说明：Svc网络模块工作函数
 ***************************/
void Svc()
{	
	//初始化工作
	
	g_bIsNetRunning=TRUE;
	struct NetCoreContext *pNetCtx = NULL;
    pNetCtx = NetCoreInit(0, NULL); //获得NetCoreContext的指针分配内存
    NetCoreIO_t io;					 //定义一个结构体io
    io.init =NetCoreIoInit;		
    io.fini =NetCoreIoFini;		
    io.process=NetCoreIoProcess;
	io.port=		9008;  		//9008  写死
	io.udp_port=	37778;		//37778 
    io.context = NULL;
    NetInitQualityRateCfg();  
    NetCoreAddHandler(pNetCtx, &io);  //
    while (!g_bExitNetThread)
    {
        NetCorePoll(pNetCtx, 5000);  
    }

    /*
    *网络模块结束
    */
    NetCoreFini(pNetCtx);	
	g_bIsNetRunning=false;
}






void *NetWorkService(void *arg)
{
	pthread_detach(pthread_self());
	SET_THREAD_NAME("NetWorkService");
	Svc();
	pthread_exit(NULL);
	return 0;
}





/**************************
 * 函数说明：开启线程，启动Svc网络模块
 ***************************/
void NetWorkService_Init()
{
	
	g_bExitNetThread=FALSE;
	pthread_create(&g_NetWork_PthreadID,NULL,NetWorkService,NULL);

}



/**************************
 * 函数说明：Svc网络模块停止工作
 ***************************/
void Net_WorkService_Stop()
{
	if(g_bIsNetRunning==TRUE)
		{
		g_bExitNetThread=TRUE;
		pthread_cancel(g_NetWork_PthreadID);
		g_bIsNetRunning=FALSE;
		}
}






void NetSvc_Init()
{
	
	Net_gCaptureList_Init(); //初始化表头，丢到NetWorkService_Init中
	Net_AudioList_init();
	NetWorkService_Init();	
}















